import {
    loginApi,
    getAuthRouters,
    loginOut
} from "../../api/login.js";

const state = {
    token: localStorage.getItem('token') || '',
    getRoutes: [],
    getReviewUser: []
}

const getters = {
    getRoutes: state => {
        return state.getRoutes
    }
}

const mutations = {
    getRoutes(state, getRoutes) {
        state.getRoutes.push(getRoutes)
    },
    clearToken(state, token) {
        state.token = token
    },
}


const actions = {
    login({
        commit
    }, params) {
        return new Promise((resolve, reject) => {
            loginApi(params).then(res => {
                if (res.code == 200) {
                    this.state.login.token = res.data.accessToken
                    localStorage.setItem('token', res.data.accessToken)
                }
                resolve(res)
            })
        })
    },
    getNewRoutes({
        commit
    }) {
        return new Promise((resolve) => {
            getAuthRouters().then(res => {
                if (res.code == 200) {
                    this.state.login.getRoutes = res.data
                    resolve(res)
                }
            })
        })
    },
    loginOut() {
        return new Promise((resolve) => {
            loginOut().then(res => {
                if (res.code == 200) {
                    this.state.login.token = '';
                    localStorage.removeItem('token');
                    resolve(res)
                }
            })
        })
    }
}

export default {
    namespaced: true,
    state,
    getters,
    mutations,
    actions,
}